# -*- coding: utf-8 -*-

# Scrapy settings for girl project
#
# For simplicity, this file contains only settings considered important or
# commonly used. You can find more settings consulting the documentation:
#
#     http://doc.scrapy.org/en/latest/topics/settings.html
#     http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
#     http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html
import os
import platform
import datetime
BOT_NAME = 'girl'
# DOWMLOAD_DELY = 5
SPIDER_MODULES = ['girl.spiders']
NEWSPIDER_MODULE = 'girl.spiders'
HTTPCACHE_POLICY = 'girl.extensions.cache.CustomPolicy'
# DEPTH_LIMIT = 0
DEPTH_STATS_VERBOSE = True
# Crawl responsibly by identifying yourself (and your website) on the user-agent
#USER_AGENT = 'girl (+http://www.yourdomain.com)'

# Obey robots.txt rules
ROBOTSTXT_OBEY = False

# Configure maximum concurrent requests performed by Scrapy (default: 16)
CONCURRENT_REQUESTS = 100

# Configure a delay for requests for the same website (default: 0)
# See http://scrapy.readthedocs.org/en/latest/topics/settings.html#download-delay
# See also autothrottle settings and docs
# DOWNLOAD_DELAY = 30
# The download delay setting will honor only one of:
#CONCURRENT_REQUESTS_PER_DOMAIN = 16
#CONCURRENT_REQUESTS_PER_IP = 16
DOWNLOAD_TIMEOUT = 500
# Disable cookies (enabled by default)
COOKIES_ENABLED = False

# Disable Telnet Console (enabled by default)
#TELNETCONSOLE_ENABLED = False

# Override the default request headers:
#DEFAULT_REQUEST_HEADERS = {
#   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
#   'Accept-Language': 'en',
#}

# Enable or disable spider middlewares
# See http://scrapy.readthedocs.org/en/latest/topics/spider-middleware.html
# SPIDER_MIDDLEWARES = {
#    'girl.middlewares.GirlSpiderMiddleware': 543,
# }

# Enable or disable downloader middlewares
# See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html
DOWNLOADER_MIDDLEWARES = {
   # 'scrapy.downloadermiddlewares.httpcache.HttpCacheMiddleware': 543,
   'random_useragent.RandomUserAgentMiddleware': 540,
   'scrapy.downloadermiddlewares.useragent.UserAgentMiddleware':None
}

# Enable or disable extensions
# See http://scrapy.readthedocs.org/en/latest/topics/extensions.html
#EXTENSIONS = {
#    'scrapy.extensions.telnet.TelnetConsole': None,
#}

# Configure item pipelines
# See http://scrapy.readthedocs.org/en/latest/topics/item-pipeline.html
ITEM_PIPELINES = {
   # 'girl.pipelines.GirlPipeline': 300,
   # 'girl.pipelines.MongoPipeline': 300,
   # 'girl.newdatapipelines.NewDataPipeline': 301,
   # 'girl.pipelines.MyImagesPipeline': 301,
   # 'scrapy.pipelines.images.ImagesPipeline': 800
    'girl.newdatapipelines.NewDataPipeline': 300,
}
# project_dir = os.path.abspath(os.path.dirname(__file__))
# IMAGES_STORE = os.path.join(project_dir, 'images')
# IMAGES_EXPIRES = 0
# Enable and configure the AutoThrottle extension (disabled by default)
# See http://doc.scrapy.org/en/latest/topics/autothrottle.html
AUTOTHROTTLE_ENABLED = True
# The initial download delay
# AUTOTHROTTLE_START_DELAY = 5
# The maximum download delay to be set in case of high latencies
#AUTOTHROTTLE_MAX_DELAY = 60
# The average number of requests Scrapy should be sending in parallel to
# each remote server
#AUTOTHROTTLE_TARGET_CONCURRENCY = 1.0
# Enable showing throttling stats for every response received:
#AUTOTHROTTLE_DEBUG = False

# Enable and configure HTTP caching (disabled by default)
# See http://scrapy.readthedocs.org/en/latest/topics/downloader-middleware.html#httpcache-middleware-settings
# MAIL_HOST = 'smtp.163.com'
# MAIL_PORT = 25
# MAIL_FROM = 'wanh042936@163.com'
# MAIL_PASS = '042936w'
# MAIL_USER = 'wanh042936'
#LOG_LEVEL = "WARNING"
HTTPCACHE_ENABLED = True
HTTPCACHE_DIR = 'httpcache'
#HTTPCACHE_IGNORE_HTTP_CODES = []
HTTPCACHE_STORAGE = 'scrapy.extensions.httpcache.FilesystemCacheStorage'
DIR = os.path.dirname(os.path.abspath(__file__))
USER_AGENT_LIST = DIR + '/user-agents.txt'
DEPTH_PRIORITY = 1
# SCHEDULER_DISK_QUEUE = 'scrapy.squeues.PickleFifoDiskQueue'
# SCHEDULER_MEMORY_QUEUE = 'scrapy.squeues.FifoMemoryQueue'
# LOG_STDOUT 默认: False 如果为 True，进程所有的标准输出(及错误)将会被重定向到log中。
# 例如，执行 print "hello" ，其将会在Scrapy log中显示
MYEXT_ENABLED=True      # 开启扩展
IDLE_NUMBER=300           # 配置空闲持续时间单位为 360个 ，一个时间单位为5s

# 在 EXTENSIONS 配置，激活扩展
EXTENSIONS= {
            'girl.extensions.close.RedisSpiderSmartIdleClosedExensions': 500
            # 'girl.extensions.MyLogExensions': 500
}

# redis
SCHEDULER = "scrapy_redis.scheduler.Scheduler"
START_URLS_KEY = '%(name):start_urls'
#确保所有的爬虫通过Redis去重
DUPEFILTER_CLASS = "scrapy_redis.dupefilter.RFPDupeFilter"

SCHEDULER_SERIALIZER = "scrapy_redis.picklecompat"

DUPEFILTER_DEBUG = True

#不清除Redis队列、这样可以暂停/恢复 爬取
SCHEDULER_PERSIST = True

SCHEDULER_QUEUE_CLASS = 'scrapy_redis.queue.PriorityQueue'

REDIS_START_URLS_AS_SET = True

SCHEDULER_IDLE_BEFORE_CLOSE = 1000

REDIS_HOST = 'localhost'
#REDIS_PORT = 6379
# REDIS_URL = 'redis://root:密码@主机ＩＰ:端口
# tmp = {'girl.newdatapipelines.NewDataPipeline': 300}